package org.vectrics.recruiter.job;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.Set;

import org.vectrics.common.html.Cell;
import org.vectrics.company.CompanyPersonGroupType;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.event.JobHtmlEmail;
import org.vectrics.user.Person;
import org.vectrics.user.PersonSearchCriteria;

public class JobStatusNotifyHtmlEmail extends JobHtmlEmail {
	private JobStatus jobStatus = null;
	
	public JobStatusNotifyHtmlEmail(Locale locale, Job job, JobStatus jobStatus) {
		super(locale, job);
		this.jobStatus = jobStatus;
	}

	protected void renderContent(Cell cell) {
		cell.addElement("<font style=\"font:normal 0.8em Verdana\">"
				+ this.getPhrase("phrase.email.jobstatus.notify") + "<br>" +
						this.jobStatus.getStatusType().getDescription(this.getLocale())
						+ "</font>");
		this.addStatusTransitionInfo(this.getJob().getCurrentStatus() , cell);
		this.addJobInfoTable(this.getJob(), cell);
		this.addJobStatusHistoryTable(this.getJob(), cell);
	}

	private void addStatusTransitionInfo(JobStatus jobStatus, Cell mainCell) {
		log.debug("Find approval people");
		JobStatusTypeApprovalSet approvalSet = RecruitServiceLocator.getJobProvider()
				.findJobStatusApprovals(this.getDomain());
		
		log.debug("Add approval persons for transition from the Current Status");
		Collection approvalsForCurrentStatusChange = approvalSet.getApprovalsForJobStatusType(
					jobStatus.getStatusType());
		if (approvalsForCurrentStatusChange.size() > 0) {
			mainCell.addElement("<br><br><font style=\"font:bold 0.8em Verdana\">"
					+ this.getPhrase("phrase.job.status.people.who.can.change") + "</font><br><br>");
	
			Set groupTypesForCurrentStatusChange = new HashSet();
			Iterator apprIter = approvalsForCurrentStatusChange.iterator();
			while (apprIter.hasNext()) {
				JobStatusTypeApproval approval = (JobStatusTypeApproval)apprIter.next();
				CompanyPersonGroupType groupType = approval.getCompanyPersonGroupType();
				groupTypesForCurrentStatusChange.add(groupType);
			}
			
			PersonSearchCriteria personCriteria = CoreServiceLocator.createPersonSearchCriteria();
			personCriteria.addCompany(this.getJob().getCompany());
			personCriteria.getGroupTypeList().addAll(groupTypesForCurrentStatusChange);
			Collection personsBelongingToGroups = CoreServiceLocator.getPersonService()
					.findPersons(personCriteria, 0, 1000);
			Iterator personIter = personsBelongingToGroups.iterator();
			while (personIter.hasNext()) {
				Person person = (Person)personIter.next();
				String email = null;
				if (person.getContactInfo() != null)
					email = person.getContactInfo().getEmail();
				mainCell.addElement("<font style=\"font:normal 0.8em Verdana\">"
						+ person.getName() + "&nbsp;(" + email + ")" +
								"</font><br>");
			}
		}
		
		Collection jobStatusTypesThatCanBeReached =
			RecruitServiceLocator.getJobProvider().findTransitionStatusTypes(
					jobStatus.getJob().getDomain(), jobStatus.getJob(), 
					jobStatus.getStatusType().getCode());
		if (jobStatusTypesThatCanBeReached.size() > 0) {
			Iterator nextStatusTypeIter = jobStatusTypesThatCanBeReached.iterator();
			while (nextStatusTypeIter.hasNext()) {
				JobStatusType nextJobStatusType = (JobStatusType)nextStatusTypeIter.next();
				log.debug("Adding groups that can transition to status: " + nextJobStatusType);
				Collection approvalsForNextStatusChange = approvalSet.getApprovalsForJobStatusType(
						nextJobStatusType);
				Iterator apprIter = approvalsForNextStatusChange.iterator();
				while (apprIter.hasNext()) {
					JobStatusTypeApproval approval = (JobStatusTypeApproval)apprIter.next();
					CompanyPersonGroupType groupType = approval.getCompanyPersonGroupType();

					PersonSearchCriteria personCriteria = CoreServiceLocator.createPersonSearchCriteria();
					personCriteria.addCompany(this.getJob().getCompany());
					personCriteria.addGroupType(groupType);
					Collection personsBelongingToGroup = CoreServiceLocator.getPersonService()
							.findPersons(personCriteria, 0, 1000);
					if (personsBelongingToGroup.size() > 0) {
						mainCell.addElement("<br><br><font style=\"font:bold 0.8em Verdana\">"
								+ this.getPhrase("phrase.job.status.people.who.can.transition.to" ) +": "
								+ approval.getToJobStatusType().getDescription(this.getLocale())
								+ "</font><br><br>");
						Iterator personIter = personsBelongingToGroup.iterator();
						while (personIter.hasNext()) {
							Person person = (Person)personIter.next();
							String email = null;
							if (person.getContactInfo() != null)
								email = person.getContactInfo().getEmail();
							mainCell.addElement("<font style=\"font:normal 0.8em Verdana\">"
									+ person.getName() + "&nbsp;(" + email + ")" +
											"</font><br>");
						}
					}
				}
			}
			
			log.debug("Find persons belonging to groups");
			
		}
	}	
	
}
